#include "psdcurvesdrawutil.h"



PSDCurvesDrawUtil::PSDCurvesDrawUtil() {}
void PSDCurvesDrawUtil::drawMultiCurves(QChartView* chartView,
                                        QString titleName,
                     const QVector<QVector<double>>& xDataList,
                     const QVector<QVector<double>>& yDataList)
{
    // 验证数据
    if (xDataList.size()!= 3 || yDataList.size()!= 3) {
        return;
    }
    // 定义曲线样式
    const QList<QColor> colors = {QColor(127, 127, 255, 255),QColor(179, 127, 255, 255),QColor(127, 255, 255, 255)};

    QChart* chart = new QChart();
    chart->setBackgroundBrush(Qt::transparent);
    chart->setMargins(QMargins(0, 0, 0, 0));
    chart->setTitle(titleName);
    chart->setAnimationOptions(QChart::SeriesAnimations);
    chart->legend()->setAlignment(Qt::AlignTop);
    // 创建曲线
    double minv=yDataList[0][0];
    double maxv=yDataList[0][0];
    QLineSeries* series_vlf = new QLineSeries();
    QLineSeries* series_lf = new QLineSeries();
    QLineSeries* series_hf = new QLineSeries();


    int len=xDataList[0].size();
    for (int j = 0; j < len; ++j) {
        series_vlf->append(xDataList[0][j], yDataList[0][j]);
        if(yDataList[0][j]>maxv)
            maxv=yDataList[0][j];
        if(yDataList[0][j]<minv)
            minv=yDataList[0][j];
    }
    len=xDataList[1].size();
    for (int j = 0; j < len; ++j) {
        series_lf->append(xDataList[1][j], yDataList[1][j]);
        if(yDataList[1][j]>maxv)
            maxv=yDataList[1][j];
        if(yDataList[1][j]<minv)
            minv=yDataList[1][j];
    }
    len=xDataList[2].size();
    for (int j = 0; j < len; ++j) {
        series_hf->append(xDataList[2][j], yDataList[2][j]);
        if(yDataList[2][j]>maxv)
            maxv=yDataList[2][j];
        if(yDataList[2][j]<minv)
            minv=yDataList[2][j];
    }

    // 4. 创建区域填充
    QAreaSeries* area_vlf = new QAreaSeries(series_vlf);
    area_vlf->setColor(colors[0]);
    QAreaSeries* area_lf = new QAreaSeries(series_lf);
    area_lf->setColor(colors[1]);
    QAreaSeries* area_hf = new QAreaSeries(series_hf);
    area_hf->setColor(colors[2]);

    area_vlf->setName("VLF");
    area_lf->setName("LF");
    area_hf->setName("HF");

    area_vlf->setBorderColor(Qt::transparent);
    area_lf->setBorderColor(Qt::transparent);
    area_hf->setBorderColor(Qt::transparent);
    // 添加到图表
    chart->addSeries(area_vlf);
    chart->addSeries(area_lf);
    chart->addSeries(area_hf);

    // 坐标轴配置
    QValueAxis* axisX = new QValueAxis();
    axisX->setRange(0, 0.4);  // 设置X轴范围
    axisX->setTitleText("Freq (Hz)");
    axisX->setGridLineVisible(false);

    QValueAxis* axisY = new QValueAxis();
    axisY->setTitleText("PSD (ms^2/Hz)");
    axisY->setGridLineColor(QColor(200, 200, 200, 100));
    axisY->setRange(0,maxv);
    axisY->setLabelsFont(QFont("微软雅黑", 8));

    chart->addAxis(axisX, Qt::AlignBottom);
    chart->addAxis(axisY, Qt::AlignLeft);
    // chart->legend()->show();
    chart->legend()->setVisible(true);

    // 关联所有系列到坐标轴
    for (auto series : chart->series()) {
        series->attachAxis(axisX);
        series->attachAxis(axisY);
    }

    chartView->setChart(chart);
    chartView->setRenderHint(QPainter::Antialiasing);
}
